/* model */
import { BaseSelectUserVuexPropertyEnum } from '@src/component/common/BaseSelectUser/store'
import { SelectUserComponentDepartment, SelectUserComponentUser } from '@src/component/common/BaseSelectUser/model'
import MsgModel from '@model/MsgModel'
/* type */
import { BaseSelectUserStoreDepartmentDataType } from '@src/component/common/BaseSelectUser/store'
import { 
  BaseSelectUserGetDepartmentListModel, 
  BaseSelectUserGetDepartmentListResult, 
  BaseSelectUserGetDepartmentUserListModel,
  BaseSelectUserGetDepartmentUserListResult 
} from '@src/component/common/BaseSelectUser/types'
/* store */
import { useBaseSelectUserStore } from '@src/component/common/BaseSelectUser/store/hooks/base'
/* vue */
import { computed } from 'vue'
/* util */
import { isEmpty, isFalse, isNotEmpty } from '@src/util/type'
import { isDisabledSelectItem } from '@src/component/common/BaseSelectUser/util'

/** 
 * @description 选人控件的部门数据
*/
function useBaseSelectUserStoreDepartmentData() {
  
  const store = useBaseSelectUserStore()
  const { currentDepartmentNav } = useBaseSelectUserStoreCurrentDepartmentNav()
  
  const departmentData = computed(() => store.state.departmentData)
  // 部门列表
  const departmentList = computed(() => {
    
    if (isEmpty(currentDepartmentNav.value)) {
      return []
    }
    
    return currentDepartmentNav.value?.tagChildren || []
    
  })
  // 根部门列表
  const departmentRootList = computed(() => departmentData.value.departmentRootList)
  const userList = computed(() => departmentData.value.userList)
  
  // 已删除列表
  const departmentRootDeleteList = computed(() => departmentRootList.value.filter(item => isDisabledSelectItem(item)))
  // 根部门下不包含已删除的部门列表
  const departmentRootNotDeleteList = computed(() => departmentRootList.value.filter(item => isFalse(isDisabledSelectItem(item))))
  
  // 部门列表是否为空
  const isDepartmentListEmpty = computed(() => isEmpty(departmentList.value))
  // 部门列表是否不为空
  const isDepartmentListNotEmpty = computed(() => isNotEmpty(departmentList.value))
  
  // 根部门列表是否为空
  const isDepartmentRootListEmpty = computed(() => isEmpty(departmentRootList.value))
  // 根部门列表是否不为空
  const isDepartmentRootListNotEmpty = computed(() => isNotEmpty(departmentRootList.value))
  
  const setDepartmentData = (value: BaseSelectUserStoreDepartmentDataType) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCurrentTabType, value)
  }
  
  const setDepartmentRootList = (value: SelectUserComponentDepartment[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetDepartmentDataDepartmentRootList, value)
  }
  
  const setDepartmentList = (value: SelectUserComponentDepartment[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetDepartmentDataDepartmentList, value)
  }
  
  const setUserList = (value: SelectUserComponentUser[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetDepartmentDataUserList, value)
  }
  
  return {
    departmentData,
    departmentRootList,
    departmentRootDeleteList,
    departmentRootNotDeleteList,
    departmentList,
    userList,
    
    isDepartmentRootListEmpty,
    isDepartmentRootListNotEmpty,
    isDepartmentListEmpty,
    isDepartmentListNotEmpty,
    
    setDepartmentData,
    setDepartmentRootList,
    setDepartmentList,
    setUserList,
  }
}

/** 
 * @description 选人控件 部门导航列表
*/
function useBaseSelectUserStoreDepartmentNavList() {
  
  const store = useBaseSelectUserStore()
  
  const departmentNavList = computed(() => store.state.departmentNavList)
  
  const isDepartmentNavListEmpty = computed(() => isEmpty(departmentNavList.value))
  const isDepartmentNavListNotEmpty = computed(() => isNotEmpty(departmentNavList.value))
  
  const setDepartmentNavList = (value: SelectUserComponentDepartment[] = []) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetDepartmentNavList, value)
  }
  
  return {
    isDepartmentNavListEmpty,
    isDepartmentNavListNotEmpty,
    departmentNavList,
    setDepartmentNavList
  }
}

/** 
 * @description 选人控件 当前部门导航
*/
function useBaseSelectUserStoreCurrentDepartmentNav() {
  
  const store = useBaseSelectUserStore()
  
  const currentDepartmentNav = computed(() => store.state.currentDepartmentNav)
  
  const setCurrentDepartmentNav = (value: SelectUserComponentDepartment | null = null) => {
    store.commit(BaseSelectUserVuexPropertyEnum.SetCurrentDepartmentNav, value)
  }
  
  return {
    currentDepartmentNav,
    setCurrentDepartmentNav
  }
}

/** 
 * @description 查询选人控件的部门列表
*/
function useBaseSelectUserStoreFetchDepartmentList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchDepartmentList(params: BaseSelectUserGetDepartmentListModel): Promise<MsgModel<BaseSelectUserGetDepartmentListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchDepartmentList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchDepartmentList
}

/** 
 * @description 查询选人控件 部门/人员 列表数据
*/
function useBaseSelectUserStoreFetchDepartmentUserList() {
  
  const store = useBaseSelectUserStore()
  
  function fetchDepartmentUserList(params: BaseSelectUserGetDepartmentUserListModel): Promise<MsgModel<BaseSelectUserGetDepartmentUserListResult>> {
    return (
      store.dispatch(BaseSelectUserVuexPropertyEnum.FetchDepartmentUserList, params).then(result => {
        return result
      })
    )
  }
  
  return fetchDepartmentUserList
}

export {
  useBaseSelectUserStoreDepartmentData,
  useBaseSelectUserStoreDepartmentNavList,
  useBaseSelectUserStoreCurrentDepartmentNav,
  useBaseSelectUserStoreFetchDepartmentList,
  useBaseSelectUserStoreFetchDepartmentUserList
}
